1690D - Black and White Stripe - CodeForces Solution


implementation two pointers *1000

Please click on ads to support us..

Python Code:

def ss(s,n,k):
    cost = [0]
    
    cap = [0]
    for i in s:
        if i=="W":
            cost.append(cost[-1]+1)
        else:
            cost.append(cost[-1])
        cap.append(cap[-1]+1)  
        
    
    
    x = float("inf")
    
    for i in range(n-k+1):
        j = i+k
        x  = min(x,cost[j]-cost[i])
        
    return x


for _ in range(int(input())):
    n,k = map(int,input().split())
    s = input()
    print(ss(s,n,k))

C++ Code:

// Code by Krushikesh Shashikant Pisal

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define all(x) (x).begin(), (x).end()
#define pb push_back;

ll factorial(ll num)
{

    return (num == 1 or num == 0) ? 1 : (num * factorial(num - 1)) % 1000000007;
}

bool isPrime(int n)
{
    if (n <= 1)
    {
        return false;
    }
    if (n <= 3)
    {
        return true;
    }

    if (n % 2 == 0 || n % 3 == 0)
    {
        return false;
    }
    // Using concept of prime number
    // can be represented in form of
    // (6*n + 1) or(6*n - 1) hence
    // we have to go for every multiple of 6 and
    // prime number would always be 1 less or 1 more than
    // the multiple of 6.
    for (int i = 5; i * i <= n; i = i + 6)
    {
        if (n % i == 0 || n % (i + 2) == 0)
        {
            return false;
        }
    }
    return true;
}

ll ceil_fun(ll n, ll num)
{
    ll num2 = n / num;
    if (n % num == 0)
        return num2;
    else
        return num2 + 1;
}

void krushikesh(ll s)
{
    cout<<s<<endl;

}
int main()
{
    ll T = 1;
    string ans;
    ll answer;
    cin >> T;
    while (T--)
    {
        ll n,k;
        cin>>n>>k;

        string s;
        cin>>s;


       vector<ll> v(n);
       ll counter=0;
       for(ll i=0;i<n;i++){
            if(s[i]=='W'){
                counter++;
            }
            v[i]=counter;
       }
        
        ll temp,j;
        ll min_val=INT_MAX;

       for(ll i=k-1;i<n;i++){
            j=i-(k-1);
            temp=v[i]-v[j];
            if(s[j]=='W'){
                temp++;
            }

            min_val=min(min_val,temp);
       }

       answer=min_val;
       krushikesh(answer);
    }
    return 0;
}




/*
    0 1 2 3 4 5 6 7 8 9 10 11 12 13
*/


Comments

Submit
0 Comments
More Questions

1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String